package xhwl.insurance.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import xhwl.android.common.util.StringUtil;
import xhwl.android.common.util.ToastUtil;

import xhwl.insurance.R;
import xhwl.insurance.customView.CircleImageView;
import xhwl.insurance.http.bean.resp.ForumProductResp;


/**
 * Created by fuyi on 2017/11/24.
 */

public class FindDetailsAdapter extends RecyclerView.Adapter<FindDetailsAdapter.MyViewHolder> {
    private OnItemClickLitener onItemClickLitener;
    private Context mContext;
    private ArrayList<ForumProductResp> data;

    public FindDetailsAdapter(Context mContext, ArrayList<ForumProductResp> data) {
        this.mContext = mContext;
        this.data = data;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_finddetailslist, null);
        return new MyViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.mTitle.setText(data.get(position).getPOST_TITLE());
        holder.mName.setText(data.get(position).getMEMBER_NAME());
        holder.mTime.setText(data.get(position).getISSUE_DATE());
        holder.mNum.setText(data.get(position).getREPLY_NUM());

        Glide.with(mContext).load(data.get(position).getMEMBER_PIC())
                .error(R.drawable.xiaoren).into(holder.mHead);

        if (StringUtil.isEmpty(data.get(position).getVI_PI_URL())) {
            holder.mPicture.setVisibility(View.GONE);
        } else if (data.get(position).getVI_PI_URL().contains(";")) {
            holder.mPicture.setVisibility(View.VISIBLE);
            String mUrl = data.get(position).getVI_PI_URL().substring(0, data.get(position).getVI_PI_URL().lastIndexOf(";"));
            Glide.with(mContext).load(mUrl).into(holder.mPicture);
            holder.mVoice.setVisibility(View.GONE);
        } else if (data.get(position).getVI_PI_URL().contains(".mp3")) {
            holder.mPicture.setVisibility(View.GONE);
            holder.mVoice.setVisibility(View.VISIBLE);
        } else {
            holder.mPicture.setVisibility(View.VISIBLE);
            holder.mVoice.setVisibility(View.GONE);
            Glide.with(mContext).load(data.get(position).getVI_PI_URL()).into(holder.mPicture);
        }


        // 如果设置了回调，则设置点击事件
        final int pos = holder.getLayoutPosition();
        if (onItemClickLitener != null) {
            holder.mItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickLitener.onItemClick(view,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTitle, mName, mTime;
        ImageView mPicture;
        ImageView mVoice;
        CircleImageView mHead;
        TextView mNum;
        RelativeLayout mItem;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_title_fdl);
            mName = (TextView) itemView.findViewById(R.id.tv_name_fdl);
            mTime = (TextView) itemView.findViewById(R.id.tv_time_fdl);
            mPicture = (ImageView) itemView.findViewById(R.id.iv_picture_fdl);
            mHead = (CircleImageView) itemView.findViewById(R.id.iv_head_fdl);
            mNum = (TextView) itemView.findViewById(R.id.tv_num_fdl);
            mVoice = (ImageView) itemView.findViewById(R.id.iv_yy_fdl);
            mItem= (RelativeLayout) itemView.findViewById(R.id.rl_item_fxd);

        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view,int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        this.onItemClickLitener = onItemClickLitener;
    }
}
